/*
* jlb
*/
package com.jlb.item.ctrl;

import com.alibaba.fastjson.JSON;
import com.jlb.user.entity.UserMiner;
import com.jlb.user.entity.UserMinerState;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;

import com.jlb.core.entity.BeanRet;
import com.jlb.core.entity.Page;
import com.jlb.core.tools.redis.RedisUtils;
import com.jlb.item.entity.Item;
import com.jlb.item.service.ItemSV;

/**
 * 产品 控制器
 *
 * @author jlb
 */
@RestController
@RequestMapping("/item")
@Slf4j
@Api(value = "产品控制器", description = "产品控制器")
public class ItemCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private ItemSV itemSV;


    /**
     * 查询Item一个详情信息
     *
     * @param id
     * @param code         编码
     * @param categoryCode 分类编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Item一个详情信息", notes = "查询Item一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query"),
            @ApiImplicitParam(name = "categoryCode", value = "分类编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String code, String categoryCode) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        if (categoryCode == null) {
            return null;
        }
        Item item = itemSV.load(id, code, categoryCode);
        log.info(JSON.toJSONString(item));
        return BeanRet.create(true, "查询成功", item);
    }


    /**
     * 根据条件id查询Item一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Item一个详情信息", notes = "查询Item一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        Item item = itemSV.loadById(id);
        log.info(JSON.toJSONString(item));
        return BeanRet.create(true, "查询成功", item);
    }

    /**
     * 根据条件code查询Item一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Item一个详情信息", notes = "查询Item一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        Item item = itemSV.loadByCode(code);
        log.info(JSON.toJSONString(item));
        return BeanRet.create(true, "查询成功", item);
    }

    /**
     * 根据条件categoryCode查询Item一个详情信息
     *
     * @param categoryCode 分类编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Item一个详情信息", notes = "查询Item一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryCode", value = "分类编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/categoryCode/{categoryCode}")
    @ResponseBody
    public BeanRet loadByCategoryCode(@PathVariable String categoryCode) {
        if (categoryCode == null) {
            return null;
        }
        Item item = itemSV.loadByCategoryCode(categoryCode);
        log.info(JSON.toJSONString(item));
        return BeanRet.create(true, "查询成功", item);
    }


    /**
     * 查询Item信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Item信息集合", notes = "查询Item信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "createTimeBegin", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeBegin", value = "更新时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeEnd", value = "更新时间", paramType = "query")
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore Item item, Integer curPage, Integer pageSize) {
        Page<Item> page = new Page<Item>(pageSize, curPage);
        List<Item> items = itemSV.list(item, page.genRowStart(), page.getPageSize());
        page.setVoList(items);
        int total = itemSV.count(item);
        page.setTotalRow(total);

        return BeanRet.create(true, "查询成功", page);

    }


    /**
     * 查询Item信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Item信息集合", notes = "查询Item信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "categoryCode", value = "分类编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, String code, String categoryCode, Integer curPage, Integer pageSize) {
        Page<Item> page = new Page<Item>(pageSize, curPage);
        List<Item> items = itemSV.list(id, code, categoryCode, page.genRowStart(), page.getPageSize());
        page.setVoList(items);
        int total = itemSV.count(id, code, categoryCode);
        page.setTotalRow(total);

        return BeanRet.create(true, "查询成功", page);
    }

    /**
     * 统计Item信息数量根据主键
     *
     * @return 总条数
     */
    @ApiOperation(value = " 统计Item信息数量根据主键", notes = " 统计Item信息数量根据主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "categoryCode", value = "分类编码", paramType = "query")
    })
    @GetMapping(value = "/count/by")
    @ResponseBody
    public Integer count(Long id, String code, String categoryCode) {
        return itemSV.count(id, code, categoryCode);
    }

    /**
     * 统计Item信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计Item信息数量", notes = "统计Item信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "categoryCode", value = "分类编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "产品名", paramType = "query"),
            @ApiImplicitParam(name = "imageUrl", value = "图片地址", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "摘要描述", paramType = "query"),
            @ApiImplicitParam(name = "jlb", value = "金兰宝价格", paramType = "query"),
            @ApiImplicitParam(name = "cost", value = "成本价", paramType = "query"),
            @ApiImplicitParam(name = "memberPrice", value = "会员价", paramType = "query"),
            @ApiImplicitParam(name = "discount", value = "折扣", paramType = "query"),
            @ApiImplicitParam(name = "storeNum", value = "库存量", paramType = "query"),
            @ApiImplicitParam(name = "isDiscount", value = "是否促销 Y,N", paramType = "query"),
            @ApiImplicitParam(name = "limitNum", value = "限购数量", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态:待审核 audited，上架 up_shelves，下架down_shelves，售罄 sold_out", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query")
    })
    @GetMapping(value = "/count")
    @ResponseBody
    public Integer count(@ApiIgnore Item item) {
        if (item == null) {
            return itemSV.count(new HashMap());
        } else {
            return itemSV.count(item);
        }
    }


    /**
     * 创建Item
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建Item", notes = "创建Item")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "产品名", paramType = "query"),
            @ApiImplicitParam(name = "imageUrl", value = "图片地址", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "摘要描述", paramType = "query"),
            @ApiImplicitParam(name = "jlb", value = "金兰宝价格", paramType = "query"),
            @ApiImplicitParam(name = "cost", value = "成本价", paramType = "query"),
            @ApiImplicitParam(name = "memberPrice", value = "会员价", paramType = "query"),
            @ApiImplicitParam(name = "discount", value = "折扣", paramType = "query"),
            @ApiImplicitParam(name = "storeNum", value = "库存量", paramType = "query"),
            @ApiImplicitParam(name = "isDiscount", value = "是否促销 Y,N", paramType = "query"),
            @ApiImplicitParam(name = "limitNum", value = "限购数量", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态:待审核 audited，上架 up_shelves，下架down_shelves，售罄 sold_out", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore Item item) {
        itemSV.save(item);
        return BeanRet.create(true, "创建成功", item);
    }


    /**
     * 修改Item
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改Item", notes = "修改Item")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "categoryCode", value = "分类编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "产品名", paramType = "query"),
            @ApiImplicitParam(name = "imageUrl", value = "图片地址", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "摘要描述", paramType = "query"),
            @ApiImplicitParam(name = "jlb", value = "金兰宝价格", paramType = "query"),
            @ApiImplicitParam(name = "cost", value = "成本价", paramType = "query"),
            @ApiImplicitParam(name = "memberPrice", value = "会员价", paramType = "query"),
            @ApiImplicitParam(name = "discount", value = "折扣", paramType = "query"),
            @ApiImplicitParam(name = "storeNum", value = "库存量", paramType = "query"),
            @ApiImplicitParam(name = "isDiscount", value = "是否促销 Y,N", paramType = "query"),
            @ApiImplicitParam(name = "limitNum", value = "限购数量", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态:待审核 audited，上架 up_shelves，下架down_shelves，售罄 sold_out", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore Item item) {
        itemSV.modify(item);
        return BeanRet.create(true, "修改成功", item);
    }

    /**
     * 删除Item
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除Item", notes = "删除Item")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "categoryCode", value = "分类编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code, String categoryCode) {
        itemSV.delete(id, code, categoryCode);
        return BeanRet.create(true, "删除Item成功");
    }

}
